package com.xh.bussiness.tms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.tms.model.ClassCourse;
import com.xh.bussiness.tms.service.IClassCourseService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/classCourse")
public class ClassCourseController {

    @Autowired
    private IClassCourseService classCourseService;


    @ApiOperation(value = "查询带分页的班级课程关系",notes = "查询带分页的班级课程关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ClassId", value="班级ID", required = true, dataType = "String"),
            @ApiImplicitParam(name="CourseId", value="课程ID", required = true, dataType = "String")
    })
    @PostMapping("/queryClassCourseList")
    public AjaxResult queryClassCourseList(XhPage page, ClassCourse bean){

        QueryWrapper qw=new QueryWrapper<ClassCourse>();
        qw.eq("class_id",bean.getClassId());
        qw.eq("course_id",bean.getCourseId());

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,classCourseService.selectPage(page,qw,ClassCourse.class));
    }


    @ApiOperation(value = "新增或者更新的班级课程关系",notes = "新增或者更新的班级课程关系")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@Validated ClassCourse bean){
        if(bean.getId()==null){
            classCourseService.insert(bean);
        }else{
            classCourseService.update(bean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",bean);
    }


    @ApiOperation(value = "删除班级课程关系",notes = "删除班级课程关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ClassId", value="班级ID", required = true, dataType = "String"),
            @ApiImplicitParam(name="CourseId", value="课程ID", required = true, dataType = "String")
    })
    @PostMapping("/deleteClassCourse")
    @Transactional
    public AjaxResult deleteClassCourse(String classId,String courseId){
        if(StringUtils.isEmpty(classId) && StringUtils.isEmpty(courseId)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"无效的参数");
        }

        classCourseService.delete(classId);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    @ApiOperation(value = "批量删除班级课程关系",notes = "批量删除班级课程关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ClassId", value="班级ID", required = true, dataType = "String"),
            @ApiImplicitParam(name="CourseId", value="课程ID", required = true, dataType = "String")
    })
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        classCourseService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}
